package com.blb.day12_spring_boot.util;

import com.blb.day12_spring_boot.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyLoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获得session的user对象
        User user = (User) request.getSession().getAttribute("user");
        if(user == null){
            //没有登录，拦截，强制登录
            response.sendRedirect("/pages/login");
            return false;
        }
        //登录，就放行
        return true;
    }
}
